package com.bestcem.xm.txtanls.grpc.convert;

import cn.hutool.core.collection.CollUtil;
import com.bestcem.xm.bi.grpc.v1.services.Worktable;
import com.bestcem.xm.txtanls.service.dto.worktable.WorktableDTO;
import com.bestcem.xm.txtanls.service.dto.worktable.WorktableFromBIDTO;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static com.bestcem.xm.common.core.constant.Constants.DEFAULT_ARRAY_SIZE;

/**
 * @author liheng
 * @version v1.0
 * @date 2021/6/21 13:39
 */
public final class WorktableConvert {
    private WorktableConvert() {
    }


    /**
     * 数据源groc实体转数据源dto
     *
     * @param worktable 数据源groc实体
     * @return 数据源dto
     */
    public static WorktableDTO convertToDTO(Worktable worktable) {
        WorktableDTO worktableDTO = new WorktableDTO();
        worktableDTO.setId(worktable.getId());
        worktableDTO.setDatasourceId(worktable.getDatasourceId());
        return worktableDTO;
    }


    /**
     * 数据源groc实体 list 转数据源dto list
     *
     * @param worktables 数据源groc实体list
     * @return 数据源dto list
     */
    public static List<WorktableDTO> convertToDTO(List<Worktable> worktables) {
        if (CollUtil.isEmpty(worktables)) {
            return Collections.emptyList();
        }
        List<WorktableDTO> worktableDTOS = new ArrayList<>(DEFAULT_ARRAY_SIZE);
        for (Worktable worktable : worktables) {
            worktableDTOS.add(convertToDTO(worktable));
        }
        return worktableDTOS;

    }

    /**
     * java端返回的worktable数据转换
     *
     * @param workTableFromBIDTO 数据源dto
     * @return 数据源实体
     */
    public static com.bestcem.xm.txtanls.grpc.v1.services.Worktable convertToGrpc(WorktableFromBIDTO workTableFromBIDTO) {
        com.bestcem.xm.txtanls.grpc.v1.services.Worktable.Builder builder = com.bestcem.xm.txtanls.grpc.v1.services.Worktable.newBuilder();
        if (!ObjectUtils.isEmpty(workTableFromBIDTO.getId())) {
            builder.setId(workTableFromBIDTO.getId());
        }
        if (!ObjectUtils.isEmpty(workTableFromBIDTO.getName())) {
            builder.setName(workTableFromBIDTO.getName());
        }
        if (!CollectionUtils.isEmpty(workTableFromBIDTO.getTextFields())) {
            builder.addAllTextFields(ColumnConvert.dtoToFieldList(workTableFromBIDTO.getTextFields()));
        }
        if (!CollectionUtils.isEmpty(workTableFromBIDTO.getTimeFields())) {
            builder.addAllTimeFields(ColumnConvert.dtoToFieldList(workTableFromBIDTO.getTimeFields()));
        }
        return builder.build();

    }

    /**
     * java端返回的worktable数据转换
     *
     * @param worktableFromBIDTOS 数据源dto list
     * @return 数据源实体 list
     */
    public static List<com.bestcem.xm.txtanls.grpc.v1.services.Worktable> convertToGrpc(List<WorktableFromBIDTO> worktableFromBIDTOS) {
        List<com.bestcem.xm.txtanls.grpc.v1.services.Worktable> worktables = new ArrayList<>(worktableFromBIDTOS.size());
        for (WorktableFromBIDTO workTableFromBIDTO : worktableFromBIDTOS) {
            worktables.add(convertToGrpc(workTableFromBIDTO));
        }
        return worktables;
    }


}
